{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ef48964e",
   "metadata": {},
   "source": [
    "# 数组\n",
    "\n",
    "```{admonition} 需求\n",
    "简单理解 **数组** 的概念。\n",
    "```\n",
    "\n",
    "```{hint}\n",
    "{guilabel}`视频`\n",
    "\n",
    "- [简单了解 C++ 数组](https://www.bilibili.com/video/BV1HZ4y1D7dh)\n",
    "```\n",
    "\n",
    "计算机的内存是一个字节序列。可以将字节序列的字节从 0 开始进行编号。将这种“指明内存中位置的数字”称为 **地址**。可以将一个地址看作一种整型值。内存中的第一个字节的地址为 0，下一个字节的地址为 1，以此类推。字节（byte）的单位为 `B`，有如下单位：\n",
    "\n",
    "$$\n",
    "\\begin{cases}\n",
    "1 B = 2^3 \\text{bit}\\\\\n",
    "1 \\text{word} = 2^2 B \\\\\n",
    "1 KB = 2^{10} B\\\\\n",
    "1 MB = 2^{10} KB = 2^{20} B\\\\\n",
    "1 GB = 2^{10} MB = 2^{30} B\\\\\n",
    "1 TB = 2^{10} GB = 2^{40} B\\\\\n",
    "1 PB = 2^{10} TB = 2^{50} B\n",
    "\\end{cases}\n",
    "$$\n",
    "\n",
    "**数组**\n",
    ":    用于表示内存中连续存储的同构对象序列（即数组的元素都具有相同的类型，且各元素之间不存在内存间隙）。数组的元素从 0 开始编号。在声明中用“方括号” `[]` 表示数组。\n",
    "\n",
    "```{admonition} 使用限制\n",
    "一个具名数组的元素数目必须在编译时就已知。\n",
    "```\n",
    "\n",
    "```{important}\n",
    "数组可看作是复合型的类型，即由算术类型复合而成。\n",
    "```\n",
    "\n",
    "## 声明\n",
    "\n",
    "声明的方法同变量声明："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "78c5a022",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[1minput_line_7:2:6: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mdefinition of variable with array type needs an explicit size or an initializer\u001b[0m\n",
      " int k[]; // 声明一个要指定数组的元素个数\n",
      "\u001b[0;1;32m     ^\n",
      "\u001b[0m"
     ]
    },
    {
     "ename": "Interpreter Error",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Interpreter Error: "
     ]
    }
   ],
   "source": [
    "int k[]; // 声明一个要指定数组的元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "590ede6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "int k[2]; // 正确"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98cb5367",
   "metadata": {},
   "source": [
    "## 初始化\n",
    "\n",
    "初始化方法有："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b46c13a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "int pp[] = {2, 3}; // 赋值初始化，此处没有知道元素个数，是因为由其值推断而得\n",
    "int p2[2] = {2, 3}; // 显式指定数组元素个数\n",
    "// { } 初始化方法\n",
    "int p3[] {2, 3}; // 此处没有知道元素个数，是因为由其值推断而得\n",
    "int p4[2] {};"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8822c265",
   "metadata": {},
   "source": [
    "数组的元组个数一定要是常量：\n",
    "\n",
    "```cpp\n",
    "constexpr int max {100};\n",
    "int gai[max]; // 一个全局数组（包含 100 个 int）；“永远活跃”\n",
    "\n",
    "void f(int n) {\n",
    "    char lac[20]; // 局部数组；“活跃”至作用域结束\n",
    "    int lai[60];\n",
    "    double lad[n]; // 错误：数组大小不是常量\n",
    "}\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++11",
   "language": "C++11",
   "name": "python3100jvsc74a57bd0a2954483fe4136a94bd721da8601251ae4c667fedb6adcfe70ff0183b19bacbc"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "C++11",
   "version": "11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
